package edu.vt.ece_3574.amber_alert.AlertSettings;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ToggleButton;
import edu.vt.ece_3574.amber_alert.AmberAlertApplication;
import edu.vt.ece_3574.amber_alert.PrimaryMain;
import edu.vt.ece_3574.amber_alert.R;
import edu.vt.ece_3574.amber_alert.membership.MemberAccountSettings;
import edu.vt.ece_3574.amber_alert.membership.PrimaryAdderActivity;
import edu.vt.ece_3574.amber_alert.messaging.MessagingUtils;

public class AlertSettings extends Activity {

	ToggleButton toggle;
	Button b;
	EditText batper;
	EditText pollInt;
	String battery;
	String batLevel;
	String batsms = "BATTERY:";
	String battot;
	String intervalSms = "INTERVAL:";
	String interval;
	SharedPreferences settings;
	

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alert_settings_screen);
		settings = getSharedPreferences(AmberAlertApplication.preferencesFileName, 0);
		String batLevelString = settings.getString("batLevel", "100");
		String pollIntString = settings.getString("pollInterval", "100");

		toggle = (ToggleButton) findViewById(R.id.toggleButton1);
		toggle.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				if(toggle.isChecked())
				{
					battery = "on";
				}
				else 
				{
					battery = "off";
				}	   

			}
		});

		b = (Button) findViewById(R.id.button1);
		pollInt = (EditText) findViewById(R.id.pollInterval);
		pollInt.addTextChangedListener(new ProperInput(120));
		pollInt.setText(pollIntString);
		
		batper = (EditText) findViewById(R.id.editText1);
		batper.addTextChangedListener(new ProperInput(100));
		batper.setText(batLevelString);

		b.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String selectedNumber = PrimaryMain.selectedMember.getNumber();
				if(batper.length() != 0) {
					batLevel = batper.getText().toString();
					battot = (batsms + battery + ":" + batLevel);					
					MessagingUtils.get().sendSMS(selectedNumber, battot);
				}
				if(pollInt.length() != 0) {
					interval = pollInt.getText().toString();
					if(Integer.parseInt(interval) > 0)
						MessagingUtils.get().sendSMS(selectedNumber, intervalSms+interval);
				}
				
				SharedPreferences.Editor editor = settings.edit();
				editor.putString("batLevel", batLevel);
				editor.putString("pollInterval", interval);
				editor.commit();
				
				Intent i = new Intent(AlertSettings.this, PrimaryMain.class);
				startActivity(i);
				AlertSettings.this.finish();
			}
		});
	}
	
	class ProperInput implements TextWatcher {
		private int maxValue;
		
		public ProperInput(int maxValue) {
			this.maxValue = maxValue;
		}

		@Override
		public void afterTextChanged(Editable editable) {
			try {
				 if(Integer.parseInt(editable.toString())>maxValue)
					 editable.replace(0, editable.length(), Integer.toString(maxValue));
			}
			 catch(NumberFormatException nfe){}
		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub
			
		}
		
	}
	public void onBackPressed() { 
		Intent i = new Intent(AlertSettings.this, PrimaryMain.class);
		startActivity(i);
		finish();
		return; 
	} 
}
